//
//  SaveViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/29.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "SaveViewController.h"
#import "DataBaseManager.h"
#import "UIScrollView+EmptyDataSet.h"
@interface SaveViewController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *arr;
@end

@implementation SaveViewController
- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatTableview];
    [[DataBaseManager shareInstance] openDB];
  self.arr = [[DataBaseManager shareInstance] select];
    self.navigationItem.title = @"我的收藏";
    //重写左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"left@3x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
}
-(void)leftAction:(UINavigationItem *)le
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -创建tableview
-(void)creatTableview
{
    self.tableView =[[ UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT * 667) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView release];
}
#pragma mark -行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}
#pragma mark -cell设置
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    FoodModel *food = _arr[indexPath.row];
    cell.textLabel.text =food.dashes_name;
    cell.detailTextLabel.text = food.dishes_id;
    return cell;
}
#pragma mark -点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShikeViewController *shike =[[ ShikeViewController alloc] init];
    FoodModel *f = _arr[indexPath.row];
    shike.model = f;
   
    [self.navigationController pushViewController:shike animated:YES];
}
//编辑方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{   //如果没有父类方法 状态无法反转
    [super setEditing:editing animated:animated];
    //使用编辑按钮 控制tableview的编辑状态
    [_tableView setEditing:editing animated:animated];
    
    
    
}
//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return YES;
}
#pragma mark -编辑样式
-(UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
       return UITableViewCellEditingStyleDelete;
    
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
//通过编辑状态判断编辑操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //通过参数editingstyle判断 执行不同操作
    //删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除操作分两部分: 数据和视图  先操作数据在同步视图
        //1.从数组中删除数据
        [[DataBaseManager shareInstance] deletename:[_arr[indexPath.row] dashes_name]];
     [_arr removeObjectAtIndex:indexPath.row];
    [_tableView deleteRowsAtIndexPaths:@[indexPath]  withRowAnimation:UITableViewRowAnimationFade];
    }
}


#pragma mark - 当数据为空时显示的字符
-(NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"你还没有收藏内容哦,快快去收藏~";
    NSDictionary *str = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f]};
    return [[NSAttributedString alloc] initWithString:text attributes:str];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
